//index.js
//获取应用实例
const app = getApp()


Page({
  data: {
    //轮播图
    imgUrls: [
      "https://nwzimg.wezhan.cn/contents/sitefiles2033/10166460/images/20967688.jpg",
      "https://nwzimg.wezhan.cn/contents/sitefiles2033/10166460/images/20967689.jpg",
      "https://nwzimg.wezhan.cn/contents/sitefiles2033/10166460/images/20967690.jpg"
    ],
    logo:'../../images/logo.png', //顶部Logo
    indicatorDots: true,//轮播图小圆点
    autoplay: true,//轮播图自动切图
    interval: 5000,//轮播图自动切换时间间隔
    duration: 500,//轮播图滑动动画时长
    time:"15:20",
    sleep:false, //是否打烊
    showSleepStatus:false, //打烊弹窗
    isAppoint:false, //预约取餐
    array: [],//可预约取餐时间表
    appointTime:"",//预约取餐时间
    isiphonex:false,//是不是苹果手机

    showAdStatus: false, //优惠券领取弹窗
    showAppointStatus: false,//优惠券领取按钮
    adleft: "7%",//优惠券left样式
    reList:[],//优惠券相关
    deskId:''
  },
  onLoad: function (options) {
    // options.deskId=5
    //获取营业时间
    this.getShopTime();
    //判断是否为苹果
    var sysinfo = wx.getSystemInfoSync().windowHeight;
    if (sysinfo>700){
      this.setData({
        isiphonex:true
      })
    }
    this.setData({
      deskId:options.deskId
    })
  },

  //自助点单
  golist: function () {
    //判断是否打烊
    if(this.data.sleep){
        this.setData({
          showSleepStatus:true
        })
    }else{
      //去点餐页面
      wx.switchTab({
        url: '../list/list?model=0',
      })
    }
  },
  //获取营业时间 生成可预约取餐时间
  getShopTime:function(){
    var that = this;
    wx.showLoading({})
    //获取我的订单
    wx.request({
      url: app.globalData.apiHost+'/getShopTime?openid=' + wx.getStorageSync('openId'),
      method: 'GET',
      data: {},
      header: {
        'Accept': 'application/json'
      },
      success: function (res) {
        var openTime = res.data.data.openTime
        var closeTime = res.data.data.closeTime 
        closeTime=24;
        var timeRange = [];
        var d = new Date();
        var now_h = d.getHours()
        var now_m = d.getMinutes()
        console.log(now_h)
        //休息时间弹出图片
        //当处于9:00以前 22:00以后时
        if (now_h < openTime || now_h > closeTime-1) {
          that.setData({
            sleep:true
          })
          //从9点开始每隔10分钟
          for (let i = openTime; i < closeTime; i++) {
            for (let j = 0; j < 60; j = j + 10) {
              if (j == 0) {
                timeRange.push(i + ":00")
              } else {
                timeRange.push(i + ":" + j)
              }
            }
          }
        } else {
          //处于营业时间则需提前半小时
          console.log(now_m)
          now_m = parseInt(now_m / 10) + 3
          console.log(now_m)
          if (now_m > 5) {
            now_m = (now_m - 6) * 10
            console.log(now_m)
            now_h += 1
          } else {
            now_m = now_m * 10
          }
          for (let i = now_h; i < closeTime; i++) {
            for (let j = now_m; j < 60; j = j + 10) {
              if (j == 0) {
                timeRange.push(i + ":00")
              } else {
                timeRange.push(i + ":" + j)
              }
            }
          }
        }
        that.setData({
          array: timeRange
        })
        wx.hideLoading();
      }
    })
  },
  // 预约取餐
  letAppoint:function(){
    this.setData({
      isAppoint:true,
        showAppointStatus: true
    })
  },
  //滚动预约时间时触发
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', this.data.array[e.detail.value])
    this.setData({
      appointTime: this.data.array[e.detail.value]
    })
    // this.goAppoint(this.data.array[e.detail.value])
  },
  //取消预约取餐 关闭弹窗
  closeAP:function(){
    this.setData({
        isAppoint:false,
        showAppointStatus:false
      })
  },
  //预约点餐 确定按钮
  goAppoint: function (time) {
    this.closeAP();
    //跳转到点餐画面
    wx.switchTab({
      url: '../list/list?model=1&appointTime=' + this.data.appointTime
    })
  }
})
